<?php
namespace app\admin\controller;
use think\Controller,
	think\Session,
    think\Db;
use PHPExcel_IOFactory;
use PHPExcel;
use app\admin\library\account\UserService;
class BaseController extends Controller
{
    
    
    public $noLogin = ['orderremark','get_access_token','get_address'];
	public function __construct(){
		parent::__construct();
        $action = request()->action();
        // 不需要登录
        if(in_array($action, $this->noLogin)){
            return;
        }
    	if( !Session::get('uid','think') ){
            return $this->redirect("Login/index"); 
        }
        $nodes = $this->nodes();

        $controller = request()->controller();
        

        $role_id = Session::get('role_id');
        //足迹
        $need = [
            'Purchase'  => ['lists','orderitemlist'],
            'StockIn'   => ['lists','purchaselist'],
            'Express'   => ['lists'],
            'Express'   => ['handle2']
        ];
        if (array_key_exists($controller, $need) && in_array($action, $need[$controller])) {
            $info = [
                'controller' => request()-> controller(),
                'action'     => request()->action(),
                'acct_id'    => Session::get('uid'),
                'name'       => Session::get('name'),
                'add_time'   => time()
            ];
            Db::name('page_log')->insert($info);
        }
        $menu_url = menu_url();
        $uid = Session::get('uid');
        $user = UserService::info($uid);
        // echo '<pre/>';
        // var_dump($user);die;
        //权限判断
        // if ($role_id==0) {
        //     $role_id = 3;
        // }
        //echo $role_id;
        // if ($role_id != 10) {
        //     if ( empty($nodes[$controller][$action]) ){
        //         $this->error('请求出错:1');
        //     } else {
        //         $role_ids = $nodes[$controller][$action];
        //         if ( !in_array($role_id, $role_ids) ){
        //             // $this->error('请求出错:2');
        //             $this->error('没有权限访问');
        //         }
        //     }
        // }
        // var_dump($menu_url, $user['roles']);die;
        // role_id=0为系统超管
        $role_id = Session::get('role_id');
        // 用户是否系统超管
        if($role_id != 0){
            if (!in_array($menu_url, $user['roles'])) {
                $this->error('没有权限访问' . $menu_url);
            }
        }
        
    }
    protected function getThumb($file_path)
    {
        $parts = explode('/', $file_path);
        $file_name = array_pop($parts);

        list($name, $ext) = explode('.', $file_name);
        $thumb_path = implode('/', $parts) . '/' . $name . '_tmb.' . $ext;
        //return $thumb_path;
        if (!file_exists('../..' . $thumb_path)) {
            return $file_path;
        } else {
            return $thumb_path;
        }
    }
    private function nodes()
    {
        return [
            'Account'      => [
                'add'           => [3,6,0],
                'handle'        => [3,6,0],
                'organizationaccount'=>[6,0],
                'lists'         => [3,6,0],
                'getlist'       => [3,6,0],
                'setstatus'     => [3,6,0],
                'getsalers'     => [1,2,3,4,6,7,0],
                'setting'       => [1,2,3,4,5,6,7,0],
                'sethandle'     => [1,2,3,4,5,6,7,0],
                'setrules'      => [3,0],
                'getuserinfo'   => [1,2,3,4,5,6,7,0],
                'role'           => [3,6,0],
                'rolelist'           => [3,6,0],
                'roledelete'           => [3,6,0],
                'roleinfo'           => [3,6,0],
                'roleedit'           => [3,6,0],
                'roleadd'           => [3,6,0],
                'menu'           => [3,6,0],
                'menuadd'           => [3,6,0],
                'menudel'           => [3,6,0],
                'menuedit'           => [3,6,0],
                'menuinfo'           => [3,6,0],
                'menulist'           => [3,6,0],
                'editrole'           => [3,6,0],
            ],
            'Organization'      => [
                'add'           => [],
                'del'           => [3,0],
                'addname'       => [3,0],
                'member'        => [3,0],
                'getorganzitionlist'=> [3,0],
                'joinorganization'=> [3,0],
                'delmember'     => [3,0],
                'editmember'     => [3,0],
                'edit'          => [3,0],
                'handle'        => [3,0],
                'lists'         => [3,0],
                'getlist'       => [1,2,3,4,5,6,0],
                'setstatus'     => [3,0],
                'getsalers'     => [1,2,3,4,7,0],
                'setting'       => [1,2,3,4,5,7,0],
                'sethandle'     => [1,2,3,4,5,7,0],
            ],
            'Area'      => [
                'lists'         => [1,2,3,5,6,7,0],
                'provinces'     => [1,2,3,5,6,7,0],
                'getlist'       => [1,2,3,5,6,7,0],
                'handle'        => [1,5,6,7,0],
                'gettoplist'    => [1,2,3,4,5,6,7,0]
            ],
            'Brand'     => [
                'lists'         => [1,2,3,5,6,7,0],
                'getlist'       => [1,2,3,5,6,7,0],
                'getlistbysort' => [1,2,3,4,5,6,7,0],
                'handle'        => [1,2,3,5,6,7,0],
            ],
            'Express'   => [
                'lists'             => [1,2,3,6,7,0],
                'getlist'           => [1,2,3,6,7,0],
                'handle'            => [1,2,3,6,7,0],
                'handle2'            => [1,2,3,6,7,0],
                'changeexphandle'   => [1,2,3,6,7,0],
                'returnexphandle'   => [1,2,3,6,7,0],
                'editexpress'       => [1,2,3,6,7,0],
            ],
            'Goods'   => [
                'lists'             => [1,2,3,4,5,6,7,0],
                'stock'             => [1,2,3,4,5,6,7,0],
                'getlist'           => [1,2,3,4,5,6,7,0],
                'update'           => [1,2,3,4,5,6,7,0],
                'add'               => [1,2,5,6,7,0],
                'edit'              => [1,2,5,6,7,0],
                'goodshandle'       => [1,2,5,6,7,0],
                'catelist'          => [1,2,3,5,6,7,0],
                'getcatelist'       => [1,2,3,5,6,7,0],
                'catehandle'        => [1,2,3,5,6,7,0],
                'getcatelistbysort' => [1,2,3,4,5,6,7,0],
                'updatacate'        => [1,2,3,6,7,0],//修改商品分类
                'cleargoods'        => [1,2,3,6,7,0],
                'getwithroomlist'   => [1,2,3,4,5,6,7,0],
            ],
            'Index'     => [
                'index'         => [1,2,3,4,5,6,7,0],
                'publict'       => [1,2,3,4,5,6,7,0],
                'orderremark'   => [1,2,3,4,5,6,7,0],
                'home'          => [1,2,3,4,5,6,7,0],  
            ],
            'Orders'    => [
                'lists'         => [1,2,3,5,6,7,0],
                'getlist'       => [1,2,3,5,6,7,0],
                'detail'        => [1,2,3,5,6,7,0],
                'add'           => [1,5,6,7,0],
                'handle'        => [1,5,6,7,0],
                'itemlist'      => [1,2,3,4,5,6,7,0],
                'getitemlist'   => [1,2,3,4,5,6,7,0],
                'getiteminfo'   => [1,2,3,4,5,6,7,0],
                'getrelationlist'  => [1,2,3,4,5,6,7,0],
                'cancelitem'    => [1,5,6,7,0],
                'handlereject'  => [1,5,6,7,0],
                'handlechange'  => [1,5,6,7,0],
                'saveitem'      => [1,6,7,0],
                'changelist'    => [1,2,3,4,5,6,7,0],
                'getchangelist' => [1,2,3,4,5,6,7,0],
                'oilist'        => [3,6,0],
                'getoilist'     => [3,6,0],
                'setstatus'     => [1,4,6,7,0],
                'exportexcle'     => [3,6,0],//导出表格
                'editaddr'      =>[1,2,3,4,5,6,7,0],
                'refundlist'    =>[1,2,3,4,5,6,7,0],
                'getcancellist' =>[1,2,3,4,5,6,7,0],
                'agreecancel' =>[1,2,3,4,5,7,0],
                'finalcancel' =>[1,2,3,4,5,6,7,0],
                'returnlist' =>[1,2,3,4,5,6,7,0],
                'getreturnlist' =>[1,2,3,4,5,6,7,0],
                'agreereturn' =>[1,2,3,4,5,7,0],
                'delreturn'     => [1,2,3,4,5,7,0],
                'delcancel'     => [1,2,3,4,5,7,0],
                'finalreturn' =>[1,2,3,4,5,6,7,0],
                'getreturnlistexcel' => [1,2,3,4,5,6,7,0],
                'getcancellistexcel' => [1,2,3,4,5,6,7,0],
                'setreturnatrr' => [1,2,3,4,5,6,7,0],
                'setcancelatrr' => [1,2,3,4,5,6,7,0],
                'financelist'   => [3,6,0],
                'getfinancelist'    => [3,6,0],

            ],
            'Purchase'  => [
                'lists'         => [1,2,3,4,5,6,7,0],
                'getlist'       => [1,2,3,4,5,6,7,0],
                'orderitemlist' => [1,3,4,5,6,7,0],
                'handle'        => [1,3,4,5,6,7,0],
                'edithandle'    => [1,3,4,6,7,0],
                'cancel'        => [1,3,4,5,6,7,0],
                'del'           => [1,2,3,4,5,6,7,0]
            ],
            'Stall'    => [
                'lists'         => [1,3,4,5,6,7,0],
                'getlist'       => [1,3,4,5,6,7,0],
                'gettoplist'    => [1,4,5,6,7,0],
                'handle'        => [1,4,5,6,7,0],
            ],
            'StockIn'    => [
                'purchaselist'  => [1,2,3,6,7,0],
                'handle'        => [1,2,3,6,7,0],
                'lists'         => [1,2,3,4,6,7,0],
                'getlist'       => [1,2,3,4,6,7,0],
                'del'           => [1,2,3,4,5,6,7,0]
            ],
            'ProjectLog'    => [
                'setcomment'         => [1,2,3,4,5,6,7,0],
                'getlist'       => [1,2,3,4,5,6,7,0],
            ],
            'Warehouse'   => [
                'stock'             => [1,2,3,4,5,6,7,0],
                'warehouse_list'    => [1,2,3,4,5,6,7,0],
                'set_warehouse'     => [1,2,3,4,5,6,7,0],
                'del_warehouse'     => [1,2,3,4,5,6,7,0],
                // 库存查询
                'room'             => [1,2,3,4,5,6,7,0],
                'room_list'        => [1,2,3,4,5,6,7,0],
                'room_list_export' => [1,2,3,4,5,6,7,0],
                'room_total'       => [1,2,3,4,5,6,7,0],
                'del_room'         => [3,0],
                'room_info'      => [1,2,3,4,5,6,7,0],
                'otpurchase'       => [1,2,3,4,5,6,7,0],
                'otpurchase_list'  => [1,2,3,4,5,6,7,0],
                'del_otpurchase'   => [1,2,3,4,5,6,7,0],
                // 添加入库
                'add_otpurchase'   => [1,2,3,4,5,6,7,0],
                'set_otpurchase'   => [1,2,3,4,5,6,7,0],
                'auditing'         => [3,0],
                // 其它出库
                'otsale'           => [1,2,3,4,5,6,7,0],
                'add_otsale'       => [1,2,3,4,5,6,7,0],
                'otsale_list'      => [1,2,3,4,5,6,7,0],
                'set_otsale'       => [1,2,3,4,5,6,7,0],
                'auditing_otsale'  => [1,2,3,4,5,6,7,0],
                'del_otsale'  => [1,2,3,4,5,6,7,0],
                // 库存盘点
                'pandian'  => [1,2,3,4,5,6,7,0],
                // 入库详情
                'otpurchase_info' => [1,2,3,4,5,6,7,0],
                // 出库详情
                'otsale_info'     => [1,2,3,4,5,6,7,0],  
                
            ],
            
        ];
    }
    function getCellName($index){
        $cell = array(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA',

            'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX',

            'AY', 'AZ'
        );;
        return $cell[$index];
    }
    /**
     * 导出表格
     * @param result 表格数据
     * @param name 下载文件名
     */
    public function excel($result=null,$name=null,$head_name_list=null){
        $excel = new PHPExcel();

        //2.指定当前的sheet
        $objSheet = $excel->setActiveSheetIndex(0);
        $j =0;
        foreach ($head_name_list as $head_index => $head_name) {
            $objSheet->setCellValue($this->getCellName($j).'1', $head_name);
            $i = 2;
            foreach ($result as $result_index => $result_name) {
                $indexName = $this->getCellName($j) . $i;
                $result_name_item = isset($result_name[$head_index])?$result_name[$head_index]:null;
                $objSheet->setCellValue($indexName, $result_name_item);
                $i++;
            }
            $j++;
            
        }
        
        //4.保存文件
        $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
        //告诉浏览器以xlsx文件输出
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        //Content-Disposition 消息头指示输出内容该以何种形式展示， attachment：消息体应该被下载到本地
        //filename的值预填为下载后的文件名
        if(empty($name)){
            $time = time();
            $name = "Content-Disposition: attachment;filename=\"{$time}.xlsx\"";
        }
        header($name);
        //设置缓存存储的最大周期，超过这个时间缓存被认为过期(单位秒),
        //max-age=0 即不缓存
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }
    /**
     * 获取自己所在团队
     */
    public function organizationList(){
        $account_id = Session::get('uid');
        $account_member = Db::name('account_member')->where('account_id',$account_id)->select();
        if(!empty($account_member)){
            $organization_list = array_map(function($item){
                return $item['organization_id'];
            },collection($account_member)->toArray());
            return $organization_list;
        }else{
            return [];
        }
    }
}